package com.hy.thread;


import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * Created With IntelliJ IDEA.
 * Descriptions:
 * <p>
 * User:Mr.Du
 * Date:2024/7/14
 * Time:15:11
 */
public class TestCompletableFuture {

    public static void main(String[] args) throws Exception{
//        testCompletableFuture();
//        testAllOf();
//        testWhenComplete();
        testAllOf1();
    }

    public static void testCompletableFuture() throws Exception {
        CompletableFuture<Object> cf = new CompletableFuture<>();
        cf.whenComplete((result, ex) -> {
            System.out.println(result+":"+ex.getMessage());
        });
        cf.thenRun(() -> {
            System.out.println(1234);
        });
        cf.complete("12345");
        Object o = cf.get();
        System.out.println(o);
    }

    public static void testWhenComplete() {
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "hello!whenComplete")
                .whenComplete((res, ex) -> {
                    // res 代表返回的结果
                    // ex 的类型为 Throwable ，代表抛出的异常
                    System.out.println(res);
                    // 这里没有抛出异常所有为 null
                });

    }

    public static void testAllOf(){
        CompletableFuture<String> module1 = CompletableFuture.supplyAsync(() -> "Module 1 info");
        CompletableFuture<String> module2 = CompletableFuture.supplyAsync(() -> "Module 2 info");
        CompletableFuture<String> module3 = CompletableFuture.supplyAsync(() -> "Module 3 info");
        CompletableFuture<String> module4 = CompletableFuture.supplyAsync(() -> "Module 4 info");
        CompletableFuture<String> module5 = CompletableFuture.supplyAsync(() -> "Module 5 info");
        CompletableFuture<Void> allOf = CompletableFuture.allOf(module1, module2, module3, module4, module5);

        CompletableFuture<String> combinedFuture = allOf.thenApply(v -> {
            try {
                // 获取每个模块的结果
                String result1 = module1.get();
                String result2 = module2.get();
                String result3 = module3.get();
                String result4 = module4.get();
                String result5 = module5.get();
                // 组合结果
                return result1 + ", " + result2 + ", " + result3 + ", " + result4 + ", " + result5;
            } catch (InterruptedException | ExecutionException e) {
                throw new RuntimeException(e);
            }
        });

        combinedFuture.thenAccept(combinedResult -> System.out.println("Combined result: " + combinedResult));
    }


    public static void testAllOf1(){
        CompletableFuture<String> module1 = CompletableFuture.supplyAsync(() -> "Module 1 info");
        CompletableFuture<String> module2 = CompletableFuture.supplyAsync(() -> "Module 2 info");
        CompletableFuture<String> module3 = CompletableFuture.supplyAsync(() -> "Module 3 info");
        CompletableFuture<String> module4 = CompletableFuture.supplyAsync(() -> "Module 4 info");
        CompletableFuture<String> module5 = CompletableFuture.supplyAsync(() -> {throw new RuntimeException("123");});
        CompletableFuture.allOf(module1, module2, module3, module4, module5).join();

        try {
            String s = module1.get();
            String s1 = module2.get();
            String s2 = module3.get();
            String s3 = module4.get();
            System.out.println(s+s1+s2+s3);
//            module5.get();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }


    }
}
